﻿using log4net;
using Mangox.BLL.Mangox;
using Mangox.Common;
using Mangox.Model.MySql;
using MGXAPI.Models;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Web.Http;

namespace MGXAPI.Controllers.API.Mangox
{
    public class KongTouController : ApiController
    {
        protected static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        protected static string className = (new StackTrace()).GetFrame(1).GetMethod().ReflectedType.Name + "/";       //获取类名 


        /// <summary>
        /// 添加空投信息API
        /// http://localhost:13772/api/KongTou/AddKongTou
        /// 参数：{"Source":"0","Token":"","UserID":"","PxgID":"123","AddressType":"BTC","BTCAddress":"test","ETHAddress":""}
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultModel AddKongTou(KongTouModelRqt param)
        {
            ResultModel result = new ResultModel();
            result.Code = -1;

            log.Info(className + $"AddKongTou param-{JsonCommon.ToJson(param)}");
            try
            {
                KongTouBLL bll = new KongTouBLL();

                /********** 参数验证 *********/
                if (param == null)
                {
                    result.Msg = "参数为空";
                    return result;
                }
                
                if (string.IsNullOrWhiteSpace(param.AddressType))
                {
                    result.Msg = "请选择地址类型";
                    return result;
                }

                //验证格式？
                /*
                if (param.AddressType == "BTC")
                {
                    if (string.IsNullOrWhiteSpace(param.BTCAddress))
                    {
                        result.Msg = "ETC地址为空";
                        return result;
                    }
                }
                else if (param.AddressType == "ETH")
                {
                    if (string.IsNullOrWhiteSpace(param.ETHAddress))
                    {
                        result.Msg = "ETH地址为空";
                        return result;
                    }
                }*/

                if (string.IsNullOrWhiteSpace(param.BitAddress))
                {
                    result.Msg = "请填写币种地址";
                    return result;
                }

                if (string.IsNullOrWhiteSpace(param.PxgID))
                {
                    result.Msg = "请填写UID";
                    return result;
                }

                //检查重复
                var kt = bll.GetKongTouByAddress(param.AddressType, param.BitAddress);
                if (kt != null && kt.Count > 0)
                {
                    //地址已经存在
                    result.Msg = "address already exist";
                    return result;
                }

                param.CreateTime = DateTime.Now;

                var p = new m_kongtou();
                p.AddressType = param.AddressType;
                p.BitAddress = param.BitAddress;
                p.BTCAddress = param.BTCAddress;
                p.CreateTime = param.CreateTime;
                p.ETHAddress = param.ETHAddress;
                p.ID = param.ID;
                p.PxgID = param.PxgID;
                p.UserID = param.UserID;

                var flag = bll.AddKongTou(p);
                if (flag)
                {
                    result.Code = 0;
                    result.Msg = "保存成功";
                }
                else
                {
                    result.Msg = "保存失败";
                }
            }
            catch (Exception ex)
            {
                result.Msg = ex.ToString();
                log.Error(className + $"AddKongTou Fail-{ex.ToString()}");
            }
            return result;
        }

    }
}
